package com.example.backend.mapper;

import com.example.backend.entity.Rider;
import com.example.backend.util.StringUtil;

public class RiderMapperProvider {
    /**
     * 获取修改骑手的sql语句
     * @param rider
     * @return  sql
     */
    public String getUpdateRiderSql(Rider rider) {
        StringBuffer sql = new StringBuffer("update rider set id = #{id} ");
        if(StringUtil.isNotEmpty(rider.getPwd())){
            sql.append(" ,pwd = #{pwd} ");
        }
        if(StringUtil.isNotEmpty(rider.getName())){
            sql.append(" ,name = #{name} ");
        }
        if(StringUtil.isNotEmpty(rider.getPhone())){
            sql.append(" ,phone = #{phone} ");
        }
        if(StringUtil.isNotEmpty(rider.getGzzt())){
            sql.append(" ,gzzt = #{gzzt} ");
        }
        if(rider.getGzsr() != null){
            sql.append(" ,gzsr = #{gzsr} ");
        }
        if(rider.getMark() != null){
            sql.append(" ,mark = #{mark} ");
        }
        if(StringUtil.isNotEmpty(rider.getState())){
            sql.append(" ,state = #{state} ");
        }

        sql.append(" where id = #{id}");
        return sql.toString();
    }

    /**
     * 获取查询骑手的SQL
     * @param rider
     * @return sql
     */
    public String getSelectRidersSql(Rider rider){
        StringBuffer sql = new StringBuffer("select id,name,pwd,phone,gzzt,gzsr,mark,state from rider where 1=1 ");
        if(StringUtil.isNotEmpty(rider.getName())){
            sql.append(" and name like concat('%',#{name},'%') ");
        }
        if(StringUtil.isNotEmpty(rider.getPhone())){
            sql.append(" and phone like concat('%',#{phone},'%') ");
        }
        if(StringUtil.isNotEmpty(rider.getGzzt())){
            sql.append(" and gzzt = #{gzzt} ");
        }
        if(rider.getGzsr() != null){
            sql.append(" and gzsr = #{gzsr} ");
        }
        if(rider.getMark() != null){
            sql.append(" and mark = #{mark} ");
        }
        if(StringUtil.isNotEmpty(rider.getState())){
            sql.append(" and state = #{state} ");
        }


        return sql.toString();
    }
    public String getUpdateRiderScoreSql(Integer id) {
        return "update rider set mark = (select avg(score) from score where bescorer_id = #{id} and bescorer_type = 2) where id = #{id}";
    }
}
